package com.hula.loan4u.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.widget.LinearLayout;

import com.hula.loan4u.R;
import com.hula.loan4u.adapter.HomeBannerAdapter;
import com.hula.loan4u.adapter.HomeCategoryAdapter;
import com.hula.loan4u.adapter.HomeGoodsAdapter;
import com.hula.loan4u.base.MBaseRecyclerAdapter;
import com.hula.loan4u.bean.BannerBean;
import com.hula.loan4u.bean.CategoryBean;
import com.hula.loan4u.bean.GoodsBean;
import com.hula.loan4u.cons.UmengKey;
import com.hula.loan4u.net.NetManager;
import com.hula.loan4u.utils.AppUtils;
import com.hula.loan4u.utils.BmobAppUpdateUtil;
import com.hula.loan4u.utils.ScreenUtil;
import com.hula.loan4u.utils.ToastAlone;
import com.hula.loan4u.utils.UmengUtil;
import com.hula.loan4u.view.HomeCategoryView;
import com.hula.loan4u.view.LoopBannerView;
import com.hula.loan4u.view.refresh.XRecyclerView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;


public class MainActivity extends BaseActivity {

    @BindView(R.id.mRecyclerView)
    XRecyclerView mRecyclerView;
    HomeGoodsAdapter goodsAdapter;

    LoopBannerView bannerView;
    HomeBannerAdapter bannerAdapter;

    HomeCategoryView categoryView;
    HomeCategoryAdapter categoryAdapter;

    int page = 0;

    @Override
    protected int getContentViewResId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
        goodsAdapter = new HomeGoodsAdapter(mContext);
        mRecyclerView.setAdapter(goodsAdapter);

        //banner
        bannerView = new LoopBannerView(mContext);
        LinearLayout.LayoutParams bannerLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, ScreenUtil.getScreenWidth(mContext) / 2);
        bannerView.setLayoutParams(bannerLayoutParams);
        bannerAdapter = new HomeBannerAdapter(mContext);
        bannerView.setAdapter(bannerAdapter);
        mRecyclerView.addHeaderView(bannerView);

        //category
        categoryView = new HomeCategoryView(mContext);
        LinearLayout.LayoutParams categoryLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        categoryView.setLayoutParams(categoryLayoutParams);
        categoryAdapter = new HomeCategoryAdapter(mContext);
        categoryView.setAdapter(categoryAdapter);
        mRecyclerView.addHeaderView(categoryView);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        new BmobAppUpdateUtil().bmobUpdateApp(false, this, new BmobAppUpdateUtil.UpdateCallBack() {
            @Override
            public void forceUpdate() {
                finish();
            }
        });

        getBanner();
        getGoods(page);
    }

    private void getGoods(final int page) {
        NetManager.getInstance().getGoods(page, this, true, new NetManager.NetCallBack<List<GoodsBean>>() {
            @Override
            public void onSuccess(List<GoodsBean> data) {
                if (page == 0) {
                    goodsAdapter.setData(data);
                    mRecyclerView.refreshComplete();
                } else {
                    if (data == null || data.size() == 0) {
                        mRecyclerView.setNoMore(true);
                    } else {
                        goodsAdapter.addData(data);
                        mRecyclerView.loadMoreComplete();
                    }
                }
            }

            @Override
            public void onFail(String message) {
                mRecyclerView.reset();
                ToastAlone.show(message);
            }
        });
    }

    private void getBanner() {
        NetManager.getInstance().getBanner(this, false, new NetManager.NetCallBack<List<BannerBean>>() {
            @Override
            public void onSuccess(List<BannerBean> data) {
                bannerAdapter.setData(data);
                if (bannerView != null) {
                    bannerView.start();
                }
            }

            @Override
            public void onFail(String message) {
                ToastAlone.show(message);
            }
        });
        NetManager.getInstance().getCategory(this, false, new NetManager.NetCallBack<List<CategoryBean>>() {
            @Override
            public void onSuccess(List<CategoryBean> data) {
                categoryAdapter.setData(data);
            }

            @Override
            public void onFail(String message) {
                ToastAlone.show(message);
            }
        });
    }

    @Override
    public void onBackPressed() {
        if (AppUtils.exitApp()) {
            super.onBackPressed();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (bannerView != null) {
            bannerView.stop();
        }
    }

    @Override
    protected void setListener() {
        mRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                page = 0;
                getGoods(page);
            }

            @Override
            public void onLoadMore() {
                page++;
                getGoods(page);
            }
        });
        goodsAdapter.setRecyclerViewCallBack(new MBaseRecyclerAdapter.RecyclerViewCallBack() {
            @Override
            public void onItemClick(int position) {
                GoodsBean bean = goodsAdapter.getItem(position);
                if (!TextUtils.isEmpty(bean.contentUrl)) {
                    Map<String, String> umengData = new HashMap<>();
                    umengData.put(bean.productName, bean.contentUrl);
                    UmengUtil.onEvent(mContext, UmengKey.home_product, umengData);

                    AppUtils.openBrowser(mContext, bean.contentUrl, null);
                } else {
                    AppUtils.openBrowser(mContext, AppUtils.getTestHtml(3 + position), null);
                }
            }
        });
        categoryAdapter.setRecyclerViewCallBack(new MBaseRecyclerAdapter.RecyclerViewCallBack() {
            @Override
            public void onItemClick(int position) {
                CategoryBean bean = categoryAdapter.getItem(position);
                if (!TextUtils.isEmpty(bean.contentUrl)) {
                    Map<String, String> umengData = new HashMap<>();
                    umengData.put(bean.topic, bean.contentUrl);
                    UmengUtil.onEvent(mContext, UmengKey.home_category, umengData);

                    AppUtils.openBrowser(mContext, bean.contentUrl, null);
                } else {
                    AppUtils.openBrowser(mContext, AppUtils.getTestHtml(position), null);
                }
            }
        });
    }

}
